package com.szholly.utils.spring;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * �����ȡ��ǰ������Spring  BeanFactory
 */
@Component
public class SpringBeanFactory implements BeanFactoryAware {  

    @Autowired
    private BeanFactory beanFactoryAuto;


    private static BeanFactory beanFactory;

    @PostConstruct
    public void init() {
        this.beanFactory = beanFactoryAuto;
    }

    /**
     * ���beanName��ȡbean
     */
    public static Object getBean(String beanName) {  
         return beanFactory.getBean(beanName); 
    }  
    
    /**
     * ���beanName��ȡbean
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object getBean(Class t) {  
         return beanFactory.getBean(t);
    }  
    
    /**
     * ��ȡ�����ж�������ж������
     */
    public static String[] getBeanDefinitionNames(){
    	DefaultListableBeanFactory tem = (DefaultListableBeanFactory)beanFactory;
        return tem.getBeanDefinitionNames();
    }
    
    /**
     * ��ȡ�����ж�������ж������
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map getBeansByType(Class t){
    	DefaultListableBeanFactory tem = (DefaultListableBeanFactory)beanFactory;
        return tem.getBeansOfType(t);
    }
    
    /**
     * ���beanName��ȡbean
     */
    public static Object getBean(String beanName, Object[] para) {  
         return beanFactory.getBean(beanName, para);  
    }  

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
        SpringBeanFactory.beanFactory = beanFactory;  
    }
}